<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="8">
					<a-form-item label="标题" name="searchKey">
						<a-input v-model:value.trim="searchFormState.searchKey" placeholder="请输入标题"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
    <a-card :bordered="false">
        <s-table
            ref="table"
            :columns="columns"
            :data="loadData"
            :alert="options.alert.show"
            bordered
            :row-key="(record) => record.id"
            :tool-config="toolConfig"
            :row-selection="options.rowSelection"
        >
            <template #operator class="table-operator">
                <a-space>
                    <a-button type="primary" @click="formRef.onOpen()">
                        <template #icon><plus-outlined /></template>
                        新增
                    </a-button>
                    <a-button danger @click="deleteBatchBizBanner()">删除</a-button>
                </a-space>
            </template>
            <template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'imgId'">
					<img v-if="record.imgId" :src="sysBaseConfig.API_URL + '/dev/file/download?id=' + record.imgId" alt="avatar"
						 style="max-height: 100px; max-width: 100px"/>
				</template>
				<template v-if="column.dataIndex === 'applScene'">
					<a-tag color="blue" v-if="record.applScene===0">通用</a-tag>
					<a-tag color="green" v-if="record.applScene===1">H5</a-tag>
					<a-tag color="orange" v-if="record.applScene===2">柜子屏</a-tag>
				</template>
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)">编辑</a>
                        <a-divider type="vertical" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteBizBanner(record)">
                            <a-button type="link" danger size="small">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="onSuccess" />
</template>

<script setup name="banner">
    import { message } from 'ant-design-vue'
    import Form from './form.vue'
    import bizBannerApi from '@/api/biz/bizBannerApi'
	import sysConfig from '@/config'

    const table = ref()
    const formRef = ref()
    const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const sysBaseConfig = ref(sysConfig)


    const columns = [
        {
            title: '标题',
            dataIndex: 'title'
        },
        {
            title: '图片',
            dataIndex: 'imgId'
        },
		{
			title: '适用场景',
			dataIndex: 'applScene'
		},
        {
            title: '排序码',
            dataIndex: 'sortCode'
        },
		{
			title: '创建时间',
			dataIndex: 'createTime'
		},
    ]
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: '150px'
	})
    let selectedRowKeys = ref([])
    // 列表选择配置
    const options = {
        alert: {
            show: false,
            clear: () => {
                selectedRowKeys = ref([])
            }
        },
        rowSelection: {
            onChange: (selectedRowKey, selectedRows) => {
                selectedRowKeys.value = selectedRowKey
            }
        }
    }
    const loadData = (parameter) => {
        return bizBannerApi.bizBannerPage(Object.assign(parameter, searchFormState)).then((data) => {
            return data
        })
    }
    // 删除
    const deleteBizBanner = (record) => {
        let params = [
            {
                id: record.id
            }
        ]
        bizBannerApi.bizBannerDelete(params).then(() => {
            table.value.refresh(true)
			message.success('删除成功')
        })
    }
    // 批量删除
    const deleteBatchBizBanner = () => {
        if (selectedRowKeys.value.length < 1) {
            message.warning('请选择一条或多条数据')
            return false
        }
        const params = selectedRowKeys.value.map((m) => {
            return {
                id: m
            }
        })
        bizBannerApi.bizBannerDelete(params).then(() => {
            table.value.clearRefreshSelected()
			message.success('删除成功')
        })
    }

	const onSuccess = ()=> {
		message.success('保存成功')
		table.value.refresh(true)
	}
</script>
